<?php

namespace App\Http\Controllers\Api;

use App\Cinema;
use App\Option;
use App\OrderOriginal;
use App\Permission;
use App\ShowtimeSchedule;
use App\User;
use Auth;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Schema;
use JWTAuth;

class HomeController extends BaseController
{

    public function __construct(Request $request)
    {
        parent::__construct();
    }

    //
    public function get_token(){
        return parent::get_token();
    }

    public function get_city(Request $request){

        if(!$code=$request->input('code')){
            $data = \DB::table('citys')->where('parent','=','')->get();
            return success($data,'查询成功');
        }else{
            if (\DB::table('citys')->where('code','=',$code)->get()->count()==0){
                return error('未找到');
            }else{
                $data = \DB::table('citys')->where('parent','=',$code)->get();
                return success($data,'查询成功');
            }
        }

    }

    /**
     * 用户登录
     */
    public function login(Request $request){

        if (!$account = $request->account){
            return error('用户账号缺失');
        }

        if (!$password = $request->password){
            return error('用户密码缺失');
        }

        $info = Auth::attempt(['account' => $account, 'password' => $password]);
        if (!$info){
            return error('用户账号、密码错误');
        }

        $role_list = Auth::user()->roles;
        $permission_list = Permission::join('permission_role','permissions.id','=','permission_role.permission_id')
            ->whereIn('permission_role.role_id',$role_list)
            ->select('permissions.*')
            ->get();
        $data['token'] = JWTAuth::fromUser(Auth::user());
        $data['user_info'] = Auth::user();
        $cinema_code = Auth::user()->cinema_code;
        $data['user_info']['cinema_name'] = Cinema::where(['code'=>$cinema_code])->value('name');
        $data['permission_list'] = $permission_list;
        $user['data'] = $data['user_info'];
        logs($user,'用户'.$data['user_info']['name'].'登录成功');
        return success($data,'登录成功');
    }



    /**
     * @param Request $request
     * @return array
     * 获取选项内容
     */
    public function get_options(Request $request){
        if (!$option_type = $request->option_type){
            $res = Option::get();
        }else{
            $res = get_options($option_type);
        }

        if ($res->count()==0){
            return error('选项不存在');
        }
        return success($res,'查询成功');
    }

    /**
     * @param Request $request
     * 添加语言
     */
    public function add_language(Request $request){
        if (!$language = trim($request->language)){
            return error('语言缺失');
        }
        $res = Option::where(['options_type'=>'language','options_value'=>$language])->first();
        if ($res){
            return error('该语言已存在');
        }
        $max = Option::where(['options_type'=>'language'])->max('options_key');
        $options = \DB::table('options');
        $language_option['options_type'] = 'language';
        $language_option['options_key'] = $max+1;
        $language_option['options_value'] = $language;
        if ($id=$options->insert($language_option)){
            return success($max+1,'添加成功');
        }else{
            return error('添加失败');
        }
    }

    /**
     * @param Request $request
     * @return array
     * 验证影院账号信息
     */
    public function verify_cinema_info(Request $request){

        if (!$cinema_code = trim($request->cinema_code)){
            return ['success'=>True,'result'=>'','msg'=>''];
        }

        $account = User::where(['cinema_code'=>$cinema_code,'is_manager'=>1,'status'=>1])->value('account');
        $account = $account?$account:''; //如果有返回为空，没有返回ture，继续注册
        return ['success'=>True,'result'=>$account,'msg'=>''];

    }

    /**
     * @param Request $request
     * 验证影院注册状态
     */
    public function get_cinema_status(Request $request){
        if (!$cinema_code = trim($request->cinemacode)){
            return ['success'=>True,'result'=>False,'msg'=>'影院编码缺失'];
        }

        $count = Cinema::where('code','=',$cinema_code)->count();

        if ($count>0){
            return ['success'=>True,'result'=>False,'msg'=>'该影院已注册'];
        }

        return ['success'=>True,'result'=>True,'msg'=>'该影院可以注册'];

    }

    /**
     * @param Request $request
     * @return array
     * 引导添加影院
     */
    public function add_cinema_account(Request $request){

        if (!$cinema_code = trim($request->cinemacode)){
            return ['success'=>True,'result'=>False,'msg'=>'影院编码缺失'];
        }

        if (!$cinema_name = trim($request->cinema_name)){
            return ['success'=>True,'result'=>False,'msg'=>'影院名称缺失'];
        }


        if (!$account = trim($request->user)){
            return ['success'=>True,'result'=>False,'msg'=>'用户账号缺失'];
        }

        if (!$password = trim($request->password)){
            return ['success'=>True,'result'=>False,'msg'=>'用户密码缺失'];
        }

        $count = Cinema::where('code','=',$cinema_code)->count();

        if ($count>0){
            return ['success'=>True,'result'=>False,'msg'=>'该影院已注册'];
        }

        $verify_account = User::where(['account'=>$account,'status'=>1])->count();
        if ($verify_account>0){
            return ['success'=>True,'result'=>False,'msg'=>'该账号已存在'];
        }

        \DB::beginTransaction();
        try{

            $cinema = new Cinema();
            $cinema->code = $cinema_code;
            $cinema->name = $cinema_name;
            $cinema->chain_code = '10000000';
            $res_cinema = $cinema->save();


            $user = new User();
            $user->account = $account;
            $user->type = 2;
            $user->is_manager = 1;
            $user->chain_code = '10000000';
            $user->name = $cinema_name;
            $user->password = bcrypt($password);
            $user->cinema_code = $cinema_code;
            $user->cinema_id = $cinema->id;
            $res_user = $user->save();
            $admin = \App\Role::where(['name'=>'影院管理员','type'=>2,'status'=>0])->first();
            $user->attachRole($admin);

            if ($res_user&&$res_cinema){
                \DB::commit();
                return ['success'=>True,'result'=>True,'msg'=>'添加成功'];
            }
        }catch (\Exception $e){
            \DB::rollBack();
            return ['success'=>True,'result'=>False,'msg'=>'影院注册失败'];
        }
    }






    public function test(Request $request){

        return date('Y-m-d H:i:s');

//        OrderOriginal::get();
        //return $_ENV['MYSQL_BIN'];

//        return Carbon::tomorrow();

//        Schema::table('price_plans',function ($table){
//            $table->string('remark')->nullable();
//        });
    }
}
